<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/12/16
 * Time: 10:10
 */

require 'config.php';
require 'functions.php';
switch (@$_GET['a']){
    case 'add':
          if (!empty($_POST['uname']) && !empty($_FILES)){
              @$auth = $_COOKIE['auth'];
              if (@$auth == '超级管理员') {
              $uname = $_POST['uname'];
              $ufile = $_FILES;
              $new_name = getUploads($_FILES,'./images');
              $path = dirname($new_name);
              $name = basename($new_name);
              $path1 = $path.'/'.$name;
              $type = array('image/png','image/jpeg','image/gif');
              $sql = "insert into type(name,path,blogo) values('{$uname}','0','{$path1}')";
              mysqli_query($link,$sql);
              if (mysqli_affected_rows($link)>0){
                  mysqli_close($link);
                  if(getImages($path,$name,$type,150)){
                      echo "<script>alert('添加成功');window.location.href='plate.php'</script>";die;
                  }
              }else{
                  echo "<script>alert('添加失败');window.location.href='father_add.php';</script>";die;
              }
              }else{
                  echo "<script>alert('你目前没有权限');window.location.href='father_add.php';</script>";die;
              }
          }else{
              echo "<script>alert('内容不能为空');window.location.href='father_add.php';</script>";die;
          }
        break;
    case 'jin':
        $id = $_GET['id'];
        $sql = "update type set status = 0 where id = $id";
        $p = $_GET['p'];
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0) {
            echo "<script>alert('禁用成功');window.location.href='plate.php?p={$p}';</script>";die;
        }
        mysqli_close($link);

        break;
    case 'kai':
        $id = $_GET['id'];
        $sql = "update type set status = 1 where id = $id";
        $p = $_GET['p'];
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0) {
            echo "<script>alert('开启成功');window.location.href='plate.php?p={$p}';</script>";die;
        }
        mysqli_close($link);

        break;
    case 'fen_jin':
        $search = $_GET['search'];
        $id = $_GET['id'];
        $sql = "update type set status = 0 where id = $id";
        $p = $_GET['p'];
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0) {
            echo "<script>alert('禁用成功');window.location.href='plate.php?p={$p}&search={$search}';</script>";die;
        }
        mysqli_close($link);
    case 'fen_kai':
        $search = $_GET['search'];
        $id = $_GET['id'];
        $sql = "update type set status = 1 where id = $id";
        $p = $_GET['p'];
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0) {
            echo "<script>alert('开启成功');window.location.href='plate.php?p={$p}&search={$search}';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'add_son':
        if(!empty($_POST['uson'])&& !empty($_FILES)){
            $uson = $_POST['uson'];
            $id = $_POST['id'];
            $path = '0-'.$id;
            $uname = $_POST['uname'];
            $ufile = $_FILES;
            $new_name = getUploads($_FILES,'./images');
            $path2 = dirname($new_name);
            $name = basename($new_name);
            $path1 = $path2.'/'.$name;
            $type = array('image/png','image/jpeg','image/gif');
            $sql = "insert into type values(null,'{$uson}',1,$id,'{$path}','{$path1}')";
            mysqli_query($link,$sql);
            if (mysqli_affected_rows($link)>0){
                mysqli_close($link);
                if(getImages($path2,$name,$type,150)){
                    echo "<script>alert('添加成功');window.location.href='plate.php'</script>";die;
                }
            }else{
                echo "<script>alert('添加失败');window.location.href='plate_add.php';</script>";die;
            }
        }else{
            echo "<script>alert('内容不能为空');window.location.href='plate.php';</script>";die;
        }
        break;
    case 'father_del':
        @$auth = $_COOKIE['auth'];
        $p = $_GET['p'];
        if (@$auth == '管理员'){
            echo "<script>alert('你目前没有这权限'); window.location.href='plate.php?p={$p}'</script>";die;
        }else{
        $id = $_GET['id'];
        $p = $_GET['p'];
        $path = $_GET['path'];
        $qz = './images/';
        $path1 = basename($path);
        $max_path = trim($path1,'x_');
        $path2 = $qz.$path1;
        $max_path1 = $qz.$max_path;
        if(is_file($path2) && is_file($max_path1)){
            unlink($path2);;
            unlink($max_path1);
            $sql = "delete from type where id ='{$id}'";
            mysqli_query($link,$sql);
            if (mysqli_affected_rows($link)>0){
                echo "<script>alert('删除成功'); window.location.href='plate.php?p={$p}'</script>";die;
            }else{
                echo "<script>alert('删除失败');window.location.href='plate.php?p={$p}';</script>";die;
            }
        }else{
            echo "<script>alert('删除失败');window.location.href='plate.php?p={$p}';</script>";die;
        }}

        break;
    case 'son_del':
        $id = $_GET['id'];
        $path = $_GET['path'];
        $qz = './images/';
        $path1 = basename($path);
        $max_path = trim($path1,'x_');
        $path2 = $qz.$path1;
        $p =$_GET['p'];
        $max_path1 = $qz.$max_path;
        if(is_file($path2) && is_file($max_path1)) {
            unlink($path2);;
            unlink($max_path1);
            $sql = "delete from type where id ='{$id}'";
            mysqli_query($link, $sql);
            if (mysqli_affected_rows($link) > 0) {
                echo "<script>alert('删除成功');window.location.href='plate.php?p={$p}';</script>";die;
            }else{
                echo "<script>alert('删除失败');window.location.href='plate.php?p={$p}';</script>";die;
            }
        }else{
            echo "<script>alert('删除失败');window.location.href='plate.php?p={$p}';</script>";die;
        }
        break;
    case 'father_edit':
        if (!empty($_POST['name'])){
            @$auth = $_COOKIE['auth'];
            if (@$auth == '管理员'){
                echo "<script>alert('你目前没有这权限'); window.location.href='plate.php'</script>";die;
            }else{
        $id = $_POST['id'];
        $uname = $_POST['name'];
        $type = array('image/png','image/jpeg','image/gif');
        $logo = $_POST['logo'];
        $path =$_POST['logo1'];
        if(is_file($path) && is_file($logo)){
            unlink($path);
            unlink($logo);
            $ufile = $_FILES;
            $new_name = getUploads($_FILES,'./images');
            $path = dirname($new_name);
            $name = basename($new_name);
            $path1 = $path.'/'.$name;
            $type = array('image/png','image/jpeg','image/gif');
            $sql = "update type set name ='{$uname}',blogo = '{$path1}' where id =$id";
            mysqli_query($link,$sql);
            if (mysqli_affected_rows($link)>0){
                mysqli_close($link);
                if(getImages($path,$name,$type,150)){
                    echo "<script>alert('修改成功');window.location.href='plate.php';</script>";die;
                }
            }else{
                echo "<script>alert('修改失败1');window.location.href='plate.php';</script>";die;
            }
        }
        else{
            echo "<script>alert('修改失败');window.location.href='plate.php';</script>";die;
        }
            }
        }else{
            echo "<script>alert('内容不能为空');window.location.href='plate.php';</script>";die;
        }
  break;
    case 'son_edit':
        if (!empty($_POST['name'])){
            $id = $_POST['id'];
            $uname = $_POST['name'];
            $type = array('image/png','image/jpeg','image/gif');
            $logo = $_POST['logo'];
            $path =$_POST['logo1'];
            if(is_file($path) && is_file($logo)){
                unlink($path);
                unlink($logo);
                $ufile = $_FILES;
                $new_name = getUploads($_FILES,'./images');
                $path = dirname($new_name);
                $name = basename($new_name);
                $path1 = $path.'/'.$name;
                $type = array('image/png','image/jpeg','image/gif');
                $sql = "update type set name ='{$uname}',blogo = '{$path1}' where id =$id";
                mysqli_query($link,$sql);
                if (mysqli_affected_rows($link)>0){
                    mysqli_close($link);
                    if(getImages($path,$name,$type,150)){
                        echo "<script>alert('修改成功');window.location.href='plate.php';</script>";die;
                    }
                }else{
                    echo "<script>alert('修改失败1');window.location.href='plate.php';</script>";die;
                }
            } else{
                echo "<script>alert('修改失败');window.location.href='plate.php'</script>";die;
            }
        }else{
            echo "<script>alert('内容不能为空');window.location.href='plate.php'</script>";die;
        }
        break;
    case 'exit':
        $time = date('Y-m-d H:i:s');
        $uname = $_COOKIE['uname'];
        $sql ="update user set lastlogin = '{$time}' where userName = '{$uname}'";
        mysqli_query($link,$sql);
        if (mysqli_affected_rows($link)>0){
            setcookie('uname','',time()-1,'/');
            setcookie('auth','',time()-1,'/');
            echo "<script>alert('退出成功');window.location.href='../index.php';</script>";die;
        }
        mysqli_close($link);
        break;
    case 'search':
        if(!empty($_POST['name'])){
            $name = $_POST['name'];
            $sql = "select * from type where name like '%{$name}%'";
            $result = mysqli_query($link,$sql);
            $rows = mysqli_fetch_assoc($result);
            if($rows){
                echo "<script>window.location.href='plate.php?search={$name}'</script>";die;
            }else{
                echo "<script>alert('用户不存在');window.location.href='plate.php?search=';</script>";die;
            }

        }else{
            echo "<script>window.location.href='plate.php?search='</script>";die;
        }
        break;

}